<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

////////////////////////////////////////////////////////
// Description: Name and version of script and site url

$script_name = 'Jupiter CMS';
$script_version = '1.1.5';
//$url = "{$HTTP_SERVER_VARS['SERVER_NAME']}".str_replace(" ", "_+_-_+_", $HTTP_SERVER_VARS['PHP_SELF']);
$url = "{$_SERVER['SERVER_NAME']}".str_replace(" ", "_+_-_+_", $_SERVER['PHP_SELF']);

////////////////////////////////////////////////////////
// Description: Extract info secure & fast

//if ($HTTP_SESSION_VARS) {extract($HTTP_SESSION_VARS, EXTR_SKIP); }
if ($_SESSION) {extract($_SESSION, EXTR_SKIP); }
//if ($HTTP_COOKIE_VARS) {extract($HTTP_COOKIE_VARS, EXTR_SKIP); }
if ($_COOKIE) {extract($_COOKIE, EXTR_SKIP); }
//if ($HTTP_POST_VARS) {extract($HTTP_POST_VARS, EXTR_SKIP); }
if ($_POST) {extract($_POST, EXTR_SKIP); }
//if ($HTTP_GET_VARS) {extract($HTTP_GET_VARS, EXTR_SKIP); }
if ($_GET) {extract($_GET, EXTR_SKIP); }
//if ($HTTP_ENV_VARS) {extract($HTTP_ENV_VARS, EXTR_SKIP); }
if ($_ENV) {extract($_ENV, EXTR_SKIP); }

////////////////////////////////////////////////////////
// Description: Notice fix

if(!isset($a)) $a = NULL;	// Action fix
if(!isset($c)) $c = NULL;	// Another Action fix
if(!isset($l)) $l = NULL;	// Poll Action fix
if(!isset($d)) $d = NULL;	// Data fix
if(!isset($o)) $o = NULL;	// Other fix
if(!isset($q)) $q = NULL;	// Another Other fix
if(!isset($k)) $k = NULL;	// Another Another Other fix
if(!isset($i)) $i = NULL;	// Info fix
if(!isset($p)) $p = 1;		// Pagination fix
if(!isset($m)) $m = date('n');	// Calendar fix

if(!isset($action)) $action = NULL;
if(!isset($template_left)) $template_left = NULL;
if(!isset($session_id)) $session_id = session_id();
if(!isset($layout)) $layout = NULL;
if(!isset($layout2)) $layout2 = NULL;
if(!isset($template_right)) $template_right = NULL;
if(!isset($_SERVER['HTTP_REFERER'])) $_SERVER['HTTP_REFERER'] = NULL;
if(!isset($PHP_SELF)) $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
if(!isset($_SESSION['authorization'])) $_SESSION['authorization'] = 0;

////////////////////////////////////////////////////////
// Description: Define Authorizations

if($_SESSION['authorization'] == 0)
{
	$is_guest = 1;
	$is_user = NULL;
	$is_moderator = NULL;
	$is_administrator = NULL;
	$is_webmaster = NULL;
}

if($_SESSION['authorization'] == 1)
{
	$is_guest = NULL;
	$is_user = 1;
	$is_moderator = NULL;
	$is_administrator = NULL;
	$is_webmaster = NULL;
}

if($_SESSION['authorization'] == 2)
{
	$is_guest = NULL;
	$is_user = NULL;
	$is_moderator = 1;
	$is_administrator = NULL;
	$is_webmaster = NULL;
}

if($_SESSION['authorization'] == 3)
{
	$is_guest = NULL;
	$is_user = NULL;
	$is_moderator = NULL;
	$is_administrator = 1;
	$is_webmaster = NULL;
}

if($_SESSION['authorization'] == 4)
{
	$is_guest = NULL;
	$is_user = NULL;
	$is_moderator = NULL;
	$is_administrator = NULL;
	$is_webmaster = 1;
}

////////////////////////////////////////////////////////
// Description: Check see if user is loged in or not

if(isset($is_guest)) $is_loged_in = NULL;
if(isset($is_user) || isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)) $is_loged_in = 1;

////////////////////////////////////////////////////////
// Description: Autoexecute these querys

$badwords_db = $db->getTable("SELECT * FROM badwords");
$emoticons_db = $db->getTable("SELECT * FROM emoticons");

////////////////////////////////////////////////////////
// Function:         Message Definition 
// Description: insert messages when called

function messagedef($message)
{
	global $config, $i;
	require "language/{$config['language']}.php";

	if(!isset($output)) $output = NULL;

	if ($i == 1) $output .="$message <span class='alert'>{$language['Message desc']}</span>";
	elseif ($i == 2) $output .="$message <span class='alert'>{$language['Message desc2']}</span>";
	elseif ($i == 3) $output .="$message <span class='ok'>{$language['Message desc3']} {$_SESSION['username']}</span>";
	elseif ($i == 4) $output .="$message <span class='ok'>{$language['Message desc4']}</span>";
	elseif ($i == 5) $output .="$message <span class='alert'>{$language['Message desc5']}</span>";
	elseif ($i == 6) $output .="$message <span class='alert'>{$language['Message desc6']}</span>";

	elseif ($i == 7) $output .="$message <span class='alert'>{$language['Message desc7']}</span>";
	elseif ($i == 8) $output .="$message <span class='alert'>{$language['Message desc8']}</span>";
	elseif ($i == 9) $output .="$message <span class='alert'>{$language['Message desc9']}</span>";
	elseif ($i == 10) $output .="$message <span class='alert'>{$language['Message desc10']}</span>";
	elseif ($i == 11) $output .="$message <span class='alert'>{$language['Message desc11']}</span>";
	elseif ($i == 12) $output .="$message <span class='alert'>{$language['Message desc12']}</span>";
	elseif ($i == 13) $output .="$message <span class='alert'>{$language['Message desc13']}</span>";
	elseif ($i == 14) $output .="$message <span class='ok'>{$language['Message desc14']}</span>";
	elseif ($i == 15) $output .="$message <span class='ok'>{$language['Message desc15']}</span>";
	elseif ($i == 16) $output .="$message <span class='normal'>{$language['Message desc16']}</span>";
	elseif ($i == 17) $output .="$message <span class='alert'>{$language['Message desc17']}</span>";
	elseif ($i == 18) $output .="$message <span class='alert'>{$language['Message desc18']}</span>";
	elseif ($i == 19) $output .="$message <span class='ok'>{$language['Message desc19']}</span>";
	elseif ($i == 20) $output .="$message <span class='ok'>{$language['Message desc20']}</span>";
	elseif ($i == 21) $output .="$message <span class='alert'>{$language['Message desc21']}</span>";
	elseif ($i == 22) $output .="$message <span class='ok'>{$language['Message desc22']}</span>";
	elseif ($i == 23) $output .="$message <span class='alert'>{$language['Message desc23']}</span>";

	elseif ($i == 24) $output .="$message <span class='ok'>{$language['Message desc24']}</span>";
	elseif ($i == 25) $output .="$message <span class='ok'>{$language['Message desc25']}</span>";
	elseif ($i == 26) $output .="$message <span class='ok'>{$language['Message desc26']}</span>";
	elseif ($i == 27) $output .="$message <span class='alert'>{$language['Message desc27']}</span>";
	elseif ($i == 28) $output .="$message <span class='alert'>{$language['Message desc28']}</span>";
	elseif ($i == 29) $output .="$message <span class='alert'>{$language['Message desc29']}</span>";
	elseif ($i == 30) $output .="$message <span class='alert'>{$language['Message desc30']}</span>";
	elseif ($i == 31) $output .="$message <span class='alert'>{$language['Message desc31']}</span>";
	elseif ($i == 32) $output .="$message <span class='alert'>{$language['Message desc32']}</span>";
	elseif ($i == 33) $output .="$message <span class='alert'>{$language['Message desc33']}</span>";
	elseif ($i == 34) $output .="$message <span class='alert'>{$language['Message desc34']}</span>";

	elseif ($i == 35) $output .="$message <span class='ok'>{$language['Message desc35']}</span>";
	elseif ($i == 36) $output .="$message <span class='ok'>{$language['Message desc36']}</span>";
	elseif ($i == 37) $output .="$message <span class='ok'>{$language['Message desc37']}</span>";
	elseif ($i == 38) $output .="$message <span class='ok'>{$language['Message desc38']}</span>";

	elseif ($i == 39) $output .="$message <span class='alert'>{$language['Message desc39']}</span>";
	elseif ($i == 40) $output .="$message <span class='alert'>{$language['Message desc40']}</span>";
	elseif ($i == 41) $output .="$message <span class='ok'>{$language['Message desc41']}</span>";
	elseif ($i == 42) $output .="$message <span class='ok'>{$language['Message desc42']}</span>";
	elseif ($i == 43) $output .="$message <span class='ok'>{$language['Message desc43']}</span>";

	elseif ($i == 44) $output .="$message <span class='ok'>{$language['Message desc44']}</span>";
	elseif ($i == 45) $output .="$message <span class='ok'>{$language['Message desc45']}</span>";
	elseif ($i == 46) $output .="$message <span class='ok'>{$language['Message desc46']}</span>";

	elseif ($i == 47) $output .="$message <span class='alert'>{$language['Message desc47']}</span>";
	elseif ($i == 48) $output .="$message <span class='alert'>{$language['Message desc48']}</span>";
	elseif ($i == 49) $output .="$message <span class='alert'>{$language['Message desc49']}</span>";
	elseif ($i == 50) $output .="$message <span class='alert'>{$language['Message desc50']}</span>";
	elseif ($i == 51) $output .="$message <span class='alert'>{$language['Message desc51']}</span>";
	elseif ($i == 52) $output .="$message <span class='alert'>{$language['Message desc52']}</span>";
	elseif ($i == 53) $output .="$message <span class='ok'>{$language['Message desc53']}</span>";
	elseif ($i == 54) $output .="$message <span class='ok'>{$language['Message desc54']}</span>";
	elseif ($i == 55) $output .="$message <span class='ok'>{$language['Message desc55']}</span>";

	elseif ($i == 56) $output .="$message <span class='ok'>{$language['Message desc56']}</span>";

	elseif ($i == 57) $output .="$message <span class='ok'>{$language['Message desc57']}</span>";
	elseif ($i == 58) $output .="$message <span class='ok'>{$language['Message desc58']}</span>";

	elseif ($i == 59) $output .="$message <span class='ok'>{$language['Message desc59']}</span>";
	elseif ($i == 60) $output .="$message <span class='ok'>{$language['Message desc60']}</span>";
	elseif ($i == 61) $output .="$message <span class='ok'>{$language['Message desc61']}</span>";

	elseif ($i == 62) $output .="$message <span class='alert'>{$language['Message desc62']}</span>";
	elseif ($i == 63) $output .="$message <span class='alert'>{$language['Message desc63']}</span>";
	elseif ($i == 64) $output .="$message <span class='ok'>{$language['Message desc64']}</span>";
	elseif ($i == 65) $output .="$message <span class='alert'>{$language['Message desc65']}</span>";

	elseif ($i == 66) $output .="$message <span class='ok'>{$language['Message desc66']}</span>";
	elseif ($i == 67) $output .="$message <span class='ok'>{$language['Message desc67']}</span>";
	elseif ($i == 68) $output .="$message <span class='ok'>{$language['Message desc68']}</span>";
	elseif ($i == 69) $output .="$message <span class='ok'>{$language['Message desc69']}</span>";
	elseif ($i == 70) $output .="$message <span class='alert'>{$language['Message desc70']}</span>";
	elseif ($i == 71) $output .="$message <span class='alert'>{$language['Message desc71']}</span>";

	elseif ($i == 72) $output .="$message <span class='ok'>{$language['Message desc72']}</span>";
	elseif ($i == 73) $output .="$message <span class='ok'>{$language['Message desc73']}</span>";
	elseif ($i == 74) $output .="$message <span class='ok'>{$language['Message desc74']}</span>";
	elseif ($i == 75) $output .="$message <span class='alert'>{$language['Message desc75']}</span>";
	elseif ($i == 76) $output .="$message <span class='alert'>{$language['Message desc76']}</span>";

	elseif ($i == 77) $output .="$message <span class='alert'>{$language['Message desc77']}</span>";
	elseif ($i == 78) $output .="$message <span class='ok'>{$language['Message desc78']}</span>";
	elseif ($i == 79) $output .="$message <span class='ok'>{$language['Message desc79']}</span>";
	elseif ($i == 80) $output .="$message <span class='ok'>{$language['Message desc80']}</span>";
	elseif ($i == 81) $output .="$message <span class='ok'>{$language['Message desc81']}</span>";
	elseif ($i == 82) $output .="$message <span class='alert'>{$language['Message desc82']}</span>";
	elseif ($i == 83) $output .="$message <span class='alert'>{$language['Message desc83']}</span>";
	elseif ($i == 84) $output .="$message <span class='alert'>{$language['Message desc84']}</span>";

	elseif ($i == 85) $output .="$message <span class='ok'>{$language['Message desc85']}</span>";
	elseif ($i == 86) $output .="$message <span class='ok'>{$language['Message desc86']}</span>";
	elseif ($i == 87) $output .="$message <span class='ok'>{$language['Message desc87']}</span>";
	elseif ($i == 88) $output .="$message <span class='alert'>{$language['Message desc88']}</span>";
	elseif ($i == 89) $output .="$message <span class='alert'>{$language['Message desc89']}</span>";

	elseif ($i == 91) $output .="$message <span class='alert'>{$language['Message desc91']}</span>";
	elseif ($i == 92) $output .="$message <span class='alert'>{$language['Message desc92']}</span>";
	elseif ($i == 93) $output .="$message <span class='alert'>{$language['Message desc93']}</span>";
	elseif ($i == 94) $output .="$message <span class='alert'>{$language['Message desc94']}</span>";
	elseif ($i == 95) $output .="$message <span class='alert'>{$language['Message desc95']}</span>";

	elseif ($i == 96) $output .="$message <span class='alert'>{$language['Message desc96']}</span>";
	elseif ($i == 97) $output .="$message <span class='alert'>{$language['Message desc97']}</span>";
	elseif ($i == 98) $output .="$message <span class='alert'>{$language['Message desc98']}</span>";
	elseif ($i == 99) $output .="$message <span class='alert'>{$language['Message desc99']}</span>";
	elseif ($i == 100) $output .="$message <span class='alert'>{$language['Message desc100']}</span>";
	elseif ($i == 101) $output .="$message <span class='ok'>{$language['Message desc101']}</span>";
	elseif ($i == 102) $output .="$message <span class='alert'>{$language['Message desc102']}</span>";
	elseif ($i == 103) $output .="$message <span class='alert'>{$language['Message desc103']}</span>";
	elseif ($i == 104) $output .="$message <span class='alert'>{$language['Message desc104']}</span>";
	elseif ($i == 105) $output .="$message <span class='alert'>{$language['Message desc105']}</span>";
	elseif ($i == 106) $output .="$message <span class='ok'>{$language['Message desc106']}</span>";
	elseif ($i == 107) $output .="$message <span class='ok'>{$language['Message desc107']}</span>";
	elseif ($i == 108) $output .="$message <span class='alert'>{$language['Message desc108']}</span>";
	elseif ($i == 109) $output .="$message <span class='alert'>{$language['Message desc109']}</span>";
	elseif ($i == 110) $output .="$message <span class='ok'>{$language['Message desc110']}</span>";
	elseif ($i == 111) $output .="$message <span class='ok'>{$language['Message desc111']}</span>";
	elseif ($i == 112) $output .="$message <span class='ok'>{$language['Message desc112']}</span>";

	elseif ($i == 113) $output .="$message <span class='alert'>{$language['Message desc113']}</span>";
	elseif ($i == 114) $output .="$message <span class='ok'>{$language['Message desc114']}</span>";
	elseif ($i == 115) $output .="$message <span class='alert'>{$language['Message desc115']}</span>";
	elseif ($i == 116) $output .="$message <span class='ok'>{$language['Message desc116']}</span>";
	elseif ($i == 117) $output .="$message <span class='ok'>{$language['Message desc117']}</span>";
	elseif ($i == 118) $output .="$message <span class='alert'>{$language['Message desc118']}</span>";
	elseif ($i == 119) $output .="$message <span class='ok'>{$language['Message desc119']}</span>";
	elseif ($i == 120) $output .="$message <span class='ok'>{$language['Message desc120']}</span>";
	elseif ($i == 121) $output .="$message <span class='alert'>{$language['Message desc121']}</span>";
	elseif ($i == 122) $output .="$message <span class='alert'>{$language['Message desc122']}</span>";
	elseif ($i == 123) $output .="$message <span class='ok'>{$language['Message desc123']}</span>";
	elseif ($i == 124) $output .="$message <span class='ok'>{$language['Message desc124']}</span>";
	elseif ($i == 125) $output .="$message <span class='alert'>{$language['Message desc125']}</span>";
	elseif ($i == 126) $output .="$message <span class='ok'>{$language['Message desc126']}</span>";
	elseif ($i == 127) $output .="$message <span class='alert'>{$language['Message desc127']}</span>";
	elseif ($i == 128) $output .="$message <span class='alert'>{$language['Message desc128']}</span>";
	elseif ($i == 129) $output .="$message <span class='ok'>{$language['Message desc129']}</span>";
	elseif ($i == 130) $output .="$message <span class='ok'>{$language['Message desc130']}</span>";
	elseif ($i == 131) $output .="$message <span class='alert'>{$language['Message desc131']}</span>";
	elseif ($i == 132) $output .="$message <span class='alert'>{$language['Message desc132']}</span>";
	elseif ($i == 133) $output .="$message <span class='ok'>{$language['Message desc133']}</span>";

	elseif ($i == 134) $output .="$message <span class='alert'>{$language['Message desc134']}</span>";

	else $output .="$message <span class='normal'>{$language['Message desc00']}</span>";

return $output;
}


////////////////////////////////////////////////////////
// Function:         jcm_linebreaks
// Description: Convert \r\n and \r to \n

function jcm_linebreaks($str){ return str_replace("\r", "\n", str_replace("\r\n", "\n", $str)); }

////////////////////////////////////////////////////////
// Function:         jcm_stripslashes
// Description: Strip slashes

function jcm_stripslashes($str){ return (get_magic_quotes_gpc() == 1) ? stripslashes($str) : $str; }

////////////////////////////////////////////////////////
// Function:         jcm_strlen
// Description: A more special strlen()

function jcm_strlen($str){ return strlen(preg_replace('/&#([0-9]+);/', '!', $str)); }

////////////////////////////////////////////////////////
// Function:         jcm_htmlspecialchars
// Description: A more special htmlspecialchars()

function jcm_htmlspecialchars($str)
{
	$str = preg_replace('/&(?!#[0-9]+;)/s', '&amp;', $str);
	$str = str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $str);

return $str;
}

////////////////////////////////////////////////////////
// Function:         split_text
// Description: Split the message into tokens

function split_text($text, $start, $end)
{
	$num_spaces = 4;

	$tokens = explode($start, $text);

	$outside[] = $tokens[0];

	$num_tokens = count($tokens);
	for ($i = 1; $i < $num_tokens; ++$i)
	{
		$temp = explode($end, $tokens[$i]);
		$inside[] = $temp[0];
		$outside[] = $temp[1];
	}

	if ($num_spaces != 8 && $start == '[code]')
	{
		$spaces = str_repeat(' ', $num_spaces);
		$inside = str_replace("\t", $spaces, $inside);
	}

return array($inside, $outside);
}

////////////////////////////////////////////////////////
// Function:         check_tag_order
// Description: Check for [code] and [quote]

function check_tag_order($text,$x)
{
	// The maximum allowed quote depth
	$max_depth = 3;

	while (true)
	{
		$c_start = strpos($text, '[code]');
		$c_end = strpos($text, '[/code]');
		$q_start = strpos($text, '[quote]');
		$q_end = strpos($text, '[/quote]');

		if (preg_match('#\[quote=(&quot;|"|\'|)(.*)\\1\]#sU', $text, $matches)) $q2_start = strpos($text, $matches[0]);
		else $q2_start = 65536;

		if ($c_start === false) $c_start = 65536;
		if ($c_end === false) $c_end = 65536;
		if ($q_start === false) $q_start = 65536;
		if ($q_end === false) $q_end = 65536;

		if (min($c_start, $c_end, $q_start, $q_end, $q2_start) == 65536) break;

		$q3_start = ($q_start < $q2_start) ? $q_start : $q2_start;

		if ($q3_start < min($q_end, $c_start, $c_end))
		{
			$step = ($q_start < $q2_start) ? 7 : strlen($matches[0]);

			$cur_index += $q3_start + $step;

			if ($q_depth == $max_depth) $overflow_begin = $cur_index - $step;

			++$q_depth;
			$text = substr($text, $q3_start + $step);
		}
		else if ($q_end < min($q_start, $c_start, $c_end))
		{
			if ($q_depth == 0){ return $x = 1; exit; }

			$q_depth--;
			$cur_index += $q_end+8;

			if ($q_depth == $max_depth) $overflow_end = $cur_index;

			$text = substr($text, $q_end+8);
		}
		else if ($c_start < min($c_end, $q_start, $q_end))
		{
			$tmp = strpos($text, '[/code]');
			if ($tmp === false){ return $x = 2; exit; }
			else $text = substr($text, $tmp+7);

			$cur_index += $tmp+7;
		}
		else if ($c_end < min($c_start, $q_start, $q_end)){ return $x = 3; exit; }
	}

	if ($q_depth > 0){ return $x = 4; exit; }
	else if ($q_depth < 0){ return $x = 5; exit; }


if (isset($overflow_begin)) return array($overflow_begin, $overflow_end);
else return null;
}

////////////////////////////////////////////////////////
// Function:         truncate_url
// Description: Takes care of url

function truncate_url($url, $link = '')
{
	$full_url = $url;
	if (strpos($url, 'www.') === 0) $full_url = 'http://'.$full_url;
	else if (strpos($url, 'ftp.') === 0) $full_url = 'ftp://'.$full_url;
	else if (strpos($url, 'http://') !== 0) $full_url = 'http://'.$full_url;

	$link = ($link == '' || $link == $url) ? ((strlen($url) > 55) ? substr($url, 0 , 39).' ... '.substr($url, -10) : $url) : stripslashes($link);

return '<a href="'.$full_url.'" target="_blank">'.$link.'</a>';
}

////////////////////////////////////////////////////////
// Function:         handle_img_tag
// Description: Takes care of the image

function handle_img_tag($url)
{
	$img_tag = '<img src="'.$url.'" alt="'.htmlspecialchars($url).'" style="border: none;">';

	return $img_tag;
}

////////////////////////////////////////////////////////
// Function:         do_bbcode
// Description: Takes care of bbcode

function do_bbcode($message)
{
	$pattern[] = '#\[b\](.*?)\[/b\]#s';
	$pattern[] = '#\[i\](.*?)\[/i\]#s';
	$pattern[] = '#\[u\](.*?)\[/u\]#s';
	$pattern[] = '#\[font=(.*?)\](.*?)\[/font\]#s';
	$pattern[] = '#\[size=(.*?)\](.*?)\[/size\]#s';
	$pattern[] = '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})](.*?)\[/color\]#s';
	$pattern[] = '#\[left\](.*?)\[/left\]#s';
	$pattern[] = '#\[center\](.*?)\[/center\]#s';
	$pattern[] = '#\[right\](.*?)\[/right\]#s';
	$pattern[] = '#\[list\]#s';
	$pattern[] = '#\[/list\]#s';
	$pattern[] = '#\[\*\]#s';
	$pattern[] = '#\[image=((ht|f)tps?://)([^\s<"]*?)\\]#e';
	$pattern[] = '#\[url\](.*?)\[/url\]#e';
	$pattern[] = '#\[url=(.*?)\](.*?)\[/url\]#e';
	$pattern[] = '#\[email\](.*?)\[/email\]#';
	$pattern[] = '#\[email=(.*?)\](.*?)\[/email\]#';
	$pattern[] = '#\[video=youtube\](.*?)\[/video\]#s';
	$pattern[] = '#\[video\](.*?)\[/video\]#s';
	$pattern[] = '#\[video=google\](.*?)\[/video\]#s';
	$pattern[] = '#\[video=myspace\](.*?)\[/video\]#s';
		
	$replace[] = '<strong>$1</strong>';
	$replace[] = '<em>$1</em>';
	$replace[] = '<u>$1</u>';
	$replace[] = '<span style="font-family: $1;">$2</span>';
	$replace[] = '<span style="font-size: $1pt;">$2</span>';
	$replace[] = '<span style="color: $1">$2</span>';
	$replace[] = '<div style="text-align: left;">$1</div>';
	$replace[] = '<div style="text-align: center;">$1</div>';
	$replace[] = '<div style="text-align: right;">$1</div>';
	$replace[] = '<ul>';
	$replace[] = '</ul>';
	$replace[] = '<li>';
	$replace[] = 'handle_img_tag(\'$1$3\')';
	$replace[] = 'truncate_url(\'$1\')';
	$replace[] = 'truncate_url(\'$1\', \'$2\')';
	$replace[] = '<a href="mailto:$1">$1</a>';
	$replace[] = '<a href="mailto:$1">$2</a>';
	$replace[] = '<object width="400" height="326"><param name="movie" value="http://www.youtube.com/v/$1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="transparent" width="400" height="326"></embed></object>';
	$replace[] = '<object width="400" height="326"><param name="movie" value="http://www.youtube.com/v/$1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="transparent" width="400" height="326"></embed></object>';
	$replace[] = '<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=$1" flashvars=""></embed>';
	$replace[] = '<embed src="http://lads.myspace.com/videos/vplayer.swf" flashvars="m=$1&type=video" type="application/x-shockwave-flash" width="400" height="326"></embed>';

	// This thing takes a while! :)
	$message = preg_replace($pattern, $replace, $message);

	if (strpos($message, 'quote') !== false)
	{
		$message = preg_replace('#\[quote=(&quot;|"|\'|)(.*)\\1\]#seU', '"<div class=\"quote\"><blockquote><h2>".str_replace(\'[\', \'&#91;\', \'$2\')." escribi&oacute;:</h2>"', $message);
		$message = str_replace('[quote]', '<div class="quote"><blockquote><h1>&nbsp;</h1>', $message);
		$message = str_replace('[/quote]', '</blockquote></div>', $message);
	}

return $message;
}

////////////////////////////////////////////////////////
// Function:         do_clickable
// Description: Takes care of links

function do_clickable($message)
{
	$message = ' '.$message;

	$message = preg_replace('#([\s\(\)])(https?|ftp|news){1}://([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^"\s\(\)<\[]*)?)#ie', '\'$1\'.truncate_url(\'$2://$3\')', $message);
	$message = preg_replace('#([\s\(\)])(www|ftp)\.(([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^"\s\(\)<\[]*)?)#ie', '\'$1\'.truncate_url(\'$2.$3\', \'$2.$3\')', $message);

return substr($message, 1);
}

////////////////////////////////////////////////////////
// Function:         do_smilies
// Description: Takes care of emoticons

function do_smilies($message)
{
	global $emoticons_db;

	if($emoticons_db != FALSE)
	{
		$smilies_path = "images/emoticons";
		foreach($emoticons_db as $idx=>$emoticons)
		{
			$smiley_codes[] = $emoticons['code'];
			$smiley_files[] = $emoticons['pic'];
		}

		$message = ' '.$message.' ';

		for ($count = 0; $count <= $idx; $count++) 
			$message = preg_replace("#(?<=.\W|\W.|^\W)".preg_quote($smiley_codes[$count], '#')."(?=.\W|\W.|\W$)#m", '$1<img src="'.$smilies_path.'/'.$smiley_files[$count].'" alt="'.$smiley_codes[$count].'">$2', $message);

		return substr($message, 1, -1);
	}
	else return $message;
}

////////////////////////////////////////////////////////
// Function:         do_censor
// Description: Takes care of badwords

function do_censor($message)
{
	global $config, $badwords_db;

	if($badwords_db != FALSE)
	{
		foreach($badwords_db as $idx=>$badwords)
		{
			$badword[] = $badwords['word'];
			$wordreplacment[] = $badwords['replacement'];
		}

		$message = ' '.$message.' ';
	
		for ($count = 0; $count <= $idx; $count++)
		{
			if($config['badword_mode'] == 1) $message = preg_replace ( '#'.$badword[$count].'#i', ''.$wordreplacment[$count].'', $message );
			else $message = preg_replace ( '#'.$badword[$count].'#i', str_pad ( '', strlen ( $badword[$count] ), '*' ), $message );
		}

		return substr($message, 1, -1);
	}
	else return $message;
}

////////////////////////////////////////////////////////
// Function:         parse_char
// Description: Replaces charactars + adds bb and sm

function parse_message($message)
{
	global $config;

	$message = jcm_htmlspecialchars($message);

	if (strpos($message, '[url]') !== false && strpos($message, '[/url]') !== false)
		$message = cuttext($message,115);

	if (strpos($message, '[code]') !== false && strpos($message, '[/code]') !== false)
	{
		list($inside, $outside) = split_text($message, '[code]', '[/code]');
		$outside = array_map('trim', $outside);
		$message = implode('<">', $outside);
	}

	if ($config['badwords'] == 1)
		$message = do_censor($message);

	$message = do_clickable($message);

	if ($config['emoticons'] == 1)
		$message = do_smilies($message);

	if ($config['bbcode'] == 1 && strpos($message, '[') !== false && strpos($message, ']') !== false)
		$message = do_bbcode($message);

	$pattern = array("\n", "\t", '  ', '  ');
	$replace = array('<br>', '&nbsp; &nbsp; ', '&nbsp; ', ' &nbsp;');
	$message = str_replace($pattern, $replace, $message);

	if (isset($inside))
	{
		$outside = explode('<">', $message);
		$message = '';

		$num_tokens = count($outside);

		for ($i = 0; $i < $num_tokens; $i++)
		{
			$message .= $outside[$i];
			if (isset($inside[$i])) $message .= '<div class="code"><blockquote><h2>Code:</h2><font size="3"><pre>'.trim($inside[$i]).'</pre></font></blockquote></div>';
		}
	}

return $message;
}

////////////////////////////////////////////////////////
// Function:         dis_bb_em
// Description: Status on BBcode and Emoticons Global

function dis_bb_em($ex=false)
{
	global $config;
	require "language/{$config['language']}.php";

	if($config['bbcode'] == 1) $bbcode = "<span class='ok'>{$language['Enabled']}</span>";
	else $bbcode = "<span class='alert'>{$language['Disabled']}</span>";

	if($config['emoticons'] == 1) $emoticons = "<span class='ok'>{$language['Enabled']}</span>";
	else $emoticons = "<span class='alert'>{$language['Disabled']}</span>";

	if($config['badwords'] == 1) $badwords = "<span class='ok'>{$language['Enabled']}</span>";
	else $badwords = "<span class='alert'>{$language['Disabled']}</span>";

	if($ex == "true") $spaces = "<br><br><br><br><br><br>";
	else $spaces = "<br><br><br>";

	$output = "$spaces<a href='?n=modules/misc&a=2'>{$language['BBcode']}</a> > $bbcode<br><a href='?n=modules/misc&a=3'>{$language['Emoticons']}</a> > $emoticons<br><a href='?n=modules/misc&a=4'>{$language['Badwords']}</a> > $badwords";

return $output;
}

////////////////////////////////////////////////////////
// Function:         insertBBcode
// Description: insert bbcode

function insertBBcode($break_location = FALSE)
{
	global $config;
	require "language/{$config['language']}.php";

	$bbcodes_path = "templates/{$config['template']}/images/codes";
	$bbcode_codes = array("[b]Text[/b]","[i]Text[/i]","[u]Text[/u]","[font=verdana]Text[/font]","[size=2]Text[/size]","[color=red]Text[/color]","[left]text[/left]","[center]text[/center]","[right]text[/right]","[list][*]Text1[*]Text2[/list]","[quote=Name]Text[/quote]","[code]Text[/code]","[image=URL]","[url=URL]Text[/url]","[email=URL]Text[/email]");
	$bbcode_files = array("bold.gif","italic.gif","underline.gif","fontface.gif","fontsize.gif","color.gif","fontleft.gif","fontcenter.gif","fontright.gif","list.gif","quote.gif","code.gif","img.gif","url.gif","email.gif");
	$bbcode_alt = array($language['BBcode desc'],$language['BBcode desc2'],$language['BBcode desc3'],$language['BBcode desc4'],$language['BBcode desc5'],$language['BBcode desc6'],$language['BBcode desc7'],$language['BBcode desc8'],$language['BBcode desc9'],$language['BBcode desc10'],$language['BBcode desc11'],$language['BBcode desc12'],$language['BBcode desc13'],$language['BBcode desc14'],$language['BBcode desc15']);

	foreach($bbcode_codes as $idx=>$code)
	{
		if(!isset($h)) $h = 1;
		if(!isset($output)) $output = NULL;

		if($config['bbcode'] == 1) $output .="<a href=\"Javascript:insertext('$code')\"><img style=\"border: none;\" src=\"$bbcodes_path/{$bbcode_files[$idx]}\" alt=\"{$bbcode_alt[$idx]}\"></a>";
		else $output .="<img style=\"border: none;\" src=\"$bbcodes_path/{$bbcode_files[$idx]}\" alt=\"{$language['Disabled']}\">";

		if($h % $break_location == 0 and $break_location) $output .= "<br>";
		else $output .= "";

		$h++;
	}

return $output;
}

////////////////////////////////////////////////////////
// Function:         insertSmilies
// Description: insert smilies

function insertSmilies($break_location = FALSE)
{
	global $config, $emoticons_db;
	require "language/{$config['language']}.php";

	$smilies_path = "images/emoticons";

	if($emoticons_db != FALSE)
	{
		foreach($emoticons_db as $emoticons)
		{
			$smiley_codes[] = $emoticons['code'];
			$smiley_files[] = $emoticons['pic'];
		}

		foreach($smiley_codes as $idx=>$code)
		{
			if(!isset($h)) $h = 1;
			if(!isset($output)) $output = NULL;

			if($config['bbcode'] == 1) $output .="<a href=\"Javascript:insertext('$code')\"><img style=\"border: none;\" src=\"$smilies_path/{$smiley_files[$idx]}\" alt=\"{$smiley_codes[$idx]}\"></a>";
			else  $output .="<img style=\"border: none;\" src=\"$smilies_path/{$smiley_files[$idx]}\" alt=\"{$language['Disabled']}\">";

			if($h % $break_location == 0 and $break_location) $output .= "<br>";
			else $output .= "";

			$h++;
		}
	}
	else $output = $language['No Emoticons'];

return $output;
}

////////////////////////////////////////////////////////
// Function:         find_ip
// Description: Shows the ip

function find_ip()
{
	if (getenv('HTTP_CLIENT_IP')) $ip = getenv('HTTP_CLIENT_IP');
	elseif (getenv('HTTP_X_FORWARDED_FOR')) $ip = getenv('HTTP_X_FORWARDED_FOR');
	elseif (getenv('HTTP_X_FORWARDED')) $ip = getenv('HTTP_X_FORWARDED');
	elseif (getenv('HTTP_FORWARDED_FOR')) $ip = getenv('HTTP_FORWARDED_FOR');
	elseif (getenv('HTTP_FORWARDED')) $ip = getenv('HTTP_FORWARDED');
	else $ip = $_SERVER['REMOTE_ADDR'];

return $ip; 
}

////////////////////////////////////////////////////////
// Function:         find_browser
// Description: Shows the browser or bots

function find_browser($browser_def)
{
	if (!(strpos($browser_def,'Mozilla') === false))
	{
		if (!(strpos($browser_def,'Opera') === false)) $browser = 'Opera';
		elseif (!(strpos($browser_def,'Firefox') === false)) $browser = 'Firefox';
		elseif (!(strpos($browser_def,'Netscape') === false)) $browser = 'Netscape';
		elseif (!(strpos($browser_def,'MSIE') === false)) $browser = 'Internet Explorer';
		else $browser = 'Other browser';
	}
	else
	{
		if (!(strpos($browser_def,'Google') === false)) $browser = 'Google Bot';
		elseif (!(strpos($browser_def,'msn') === false)) $browser = 'Msn Bot';
		else $browser = 'Other bot';
	}

return $browser;
}

////////////////////////////////////////////////////////
// Function:         generate_code
// Description: This function generates a random code

function generate_code($length = 8, $seeds = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
	$str = '';
	$seeds_count = strlen($seeds);

	list($usec, $sec) = explode(' ', microtime());
	$seed = (float) $sec + ((float) $usec * 100000);
	mt_srand($seed);

	for ($i = 0; $length > $i; $i++)
		$str .= $seeds{mt_rand(0, $seeds_count - 1)};
 
return $str;
}

////////////////////////////////////////////////////////
// Function:         send_email
// Description: This function sends mails to destination

function send_email($dat)
{
	global $config, $template;

	$website_name = $config['website_name'];
	$website_url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
	$webmaster_name = $config['webmaster_name'];
	$webmaster_email = $config['webmaster_email'];

	switch($dat['reason'])
	{
		case 'activate':

			$subject = "$website_name | Activation";

			$body = implode("", file("templates/$template/email/act.php"));
			$body = str_replace("{username}", $dat['username'], $body);
			$body = str_replace("{date}", date("l, d F Y - h:i:s",$dat['actime']), $body);
			$body = str_replace("{link}", $website_url."?n=modules/register&a=2&key=".$dat['multikey'], $body);
			$body = str_replace("{key}", $dat['multikey'], $body);
			$body = str_replace("{website_name}", $website_name, $body);
			$body = str_replace("{webmaster_name}", $webmaster_name, $body);
			$body = str_replace("{webmaster_email}", $webmaster_email, $body);

		break;

		case 'forgot':

			$subject = "$website_name | Forgotten Password";

			$body = implode("", file("templates/$template/email/fpw.php"));
			$body = str_replace("{username}", $dat['username'], $body);
			$body = str_replace("{date}", date("l, d F Y - h:i:s",$dat['actime']), $body);
			$body = str_replace("{link}", $website_url."?n=modules/register&a=3&d=3&key=".$dat['multikey'], $body);
			$body = str_replace("{website_name}", $website_name, $body);
			$body = str_replace("{webmaster_name}", $webmaster_name, $body);
			$body = str_replace("{webmaster_email}", $webmaster_email, $body);

		break;
	}

	$headers ="";
	$headers.="From: \"".$webmaster_name."\" <".$webmaster_email.">\n"; 
	$headers.="Return-Path: $webmaster_email\n";
	$headers.="X-Priority: 3\n"; 
	$headers.="X-Mailer: $website_name Server\n";

	// send out the message
	mail($dat['email'], $subject, $body, $headers);
}

////////////////////////////////////////////////////////
// Function:         cuttext
// Description: Cuts a string and adds ..

function cuttext($value,$lenght)
{
	if (strlen($value) >= $lenght )
	{
		$limited = substr($value,0,$lenght);
		$limited .= "...";                 
	}
	else
		$limited = $value;

return $limited;
}

////////////////////////////////////////////////////////
// Function:         row2color
// Description: Shows the results on 2 colors

function row2color($id_def)
{
	if ($id_def/2 == round($id_def/2)) $row = "con1";
	else $row = "con2";

return $row;
}

////////////////////////////////////////////////////////
// Function:         today_yesterday
// Description: Function for Today and Yesterday

function today_yesterday($time,$ex=false)
{
	global $config;
	require "language/{$config['language']}.php";

	$today = date("d/m/y",time());
	$yesterday = date("d/m/y",time() - 86400);

	if($ex == "true")
	{
		if ($today == date("d/m/y",$time)) $date = "{$language['Time today']} ".date("h:i:s A", $time)."";
		elseif ($yesterday == date("d/m/y",$time)) $date = "{$language['Time yesterday']} ".date("h:i:s A", $time)."";
		else $date = date("d/m/y h:i:s A", $time);
	}
	else
	{
		if ($today == date("d/m/y",$time)) $date = "{$language['Time today']} - ".date("h:i:s A", $time)."";
		elseif ($yesterday == date("d/m/y",$time)) $date = "{$language['Time yesterday']} - ".date("h:i:s A", $time)."";
		else $date = date("d/m/y - h:i:s A", $time);
	}

return $date;
}

////////////////////////////////////////////////////////
// Function:         last_visit
// Description: Calculates the last visit

function last_visit($time,$regtime=false)
{
	global $config;
	require "language/{$config['language']}.php";

	if($time == $regtime)
		$finish = $language['Time Definition Never'];
	else
	{
		$day = date("d",$time);
		$month = date("m",$time);
		$year = date("Y",$time);
		$hours = date("H",$time);
		$minutes = date("i",$time);
		$seconds = date("s",$time);

		$date = time() - mktime($hours, $minutes, $seconds, $month, $day, $year);

		if (!is_array($date))
		{
			$periods = array ($language['Time Definition Years'] => 31556926,$language['Time Definition Months'] => 2629743,$language['Time Definition Days'] => 86400,$language['Time Definition Hours'] => 3600,$language['Time Definition Minutes'] => 60,$language['Time Definition Seconds'] => 1);

			$date = (int) $date;
			foreach ($periods as $period => $value)
			{
				$count = floor($date / $value);
 
				if ($count == 0)
					continue;
 
				$values[$period] = $count;
				$date = $date % $value;
			}
 
	        	if (empty($values))
				$values = null;
 
			$date = $values;
		}

		if (!is_array($date))
			return false;
 
		foreach ($date as $key => $value)
		{
			$segment = $value . ' ' . $key; 
 
			if ($value == 1)
			{
				if($key == $language['Time Definition Years']) $segment = $value . ' ' . $language['Time Definition Year'];
				if($key == $language['Time Definition Months']) $segment = $value . ' ' . $language['Time Definition Month'];
				if($key == $language['Time Definition Weeks']) $segment = $value . ' ' . $language['Time Definition Week'];
				if($key == $language['Time Definition Days']) $segment = $value . ' ' . $language['Time Definition Day'];
				if($key == $language['Time Definition Hours']) $segment = $value . ' ' . $language['Time Definition Hour'];
				if($key == $language['Time Definition Minutes']) $segment = $value . ' ' . $language['Time Definition Minute'];
				if($key == $language['Time Definition Seconds']) $segment = $value . ' ' . $language['Time Definition Second'];
			}
 
			$array[] = $segment;
		}
 
		$result = implode(', ', $array);
		$finish = $result." {$language['HQ user desc7']}";
	}

return $finish;
}

////////////////////////////////////////////////////////
// Function:         find_country
// Description: insert country for user detalies

function find_country($name=false,$type=false)
{
	if(!isset($list)) $list = NULL;

	$c = array ("none" => "Not Specified","af" => "Afghanistan","al" => "Albania","dz" => "Algeria","ao" => "Angola","ar" => "Argentina","am" => "Armenia","aw" => "Aruba","au" => "Australia","at" => "Austria","az" => "Azerbaijan","bs" => "Bahamas","bh" => "Bahrain","bd" => "Bangladesh","bb" => "Barbados","by" => "Belarus","be" => "Belgium","bz" => "Belize","bj" => "Benin","bm" => "Bermuda","bt" => "Bhutan","bo" => "Bolivia","ba" => "Bosnia And Herzegovina","bw" => "Botswana","br" => "Brazil","bn" => "Brunei Darussalam","bg" => "Bulgaria","bf" => "Burkina Faso","bi" => "Burundi","kh" => "Cambodia","cm" => "Cameroon","ca" => "Canada","cv" => "Cape Verde","ky" => "Cayman Islands","cf" => "Central African Republic","cl" => "Chile","cn" => "China","co" => "Colombia","cg" => "Congo","ck" => "Cook Islands","cr" => "Costa Rica","ci" => "Cote D'Ivoire","hr" => "Croatia","cu" => "Cuba","cy" => "Cyprus","cz" => "Czech Republic","dk" => "Denmark","tp" => "East Timor","ec" => "Ecuador","eg" => "Egypt","er" => "Eritrea","ee" => "Estonia","et" => "Ethiopia","fo" => "Faroe Islands","fj" => "Fiji","fi" => "Finland","fr" => "France","pf" => "French Polynesia","ga" => "Gabon","ge" => "Georgia","de" => "Germany","gi" => "Gibraltar","gr" => "Greece","gl" => "Greenland","gp" => "Guadeloupe","gu" => "Guam","gt" => "Guatemala","gy" => "Guyana","ht" => "Haiti","va" => "Holy See (Vatican City State)","hk" => "Hong Kong","hu" => "Hungary","is" => "Iceland","in" => "India","id" => "Indonesia","ir" => "Iran","iq" => "Iraq","ie" => "Ireland","il" => "Israel","it" => "Italy","jm" => "Jamaica","jp" => "Japan","jo" => "Jordan","kz" => "Kazakstan","ke" => "Kenya","ki" => "Kiribati","kp" => "Korea, Democratic People'S Republic Of","kr" => "Korea, Republic Of","kg" => "Kyrgyzstan","lv" => "Latvia","lb" => "Lebanon","ly" => "Libyan Arab Jamahiriya","lt" => "Lithuania","lu" => "Luxembourg","mo" => "Macau","mk" => "Macedonia","mg" => "Madagascar","my" => "Malaysia","mt" => "Malta","mx" => "Mexico","md" => "Moldova, Republic Of","mc" => "Monaco","mn" => "Mongolia","ms" => "Montserrat","ma" => "Morocco","mz" => "Mozambique","na" => "Namibia","nr" => "Nauru","np" => "Nepal","nl" => "Netherlands","an" => "Netherlands Antilles","nc" => "New Caledonia","nz" => "New Zealand","nf" => "Norfolk Island","mp" => "Northern Mariana Islands","no" => "Norway","om" => "Oman","pk" => "Pakistan","ps" => "Palestinian Territory, Occupied","pa" => "Panama","py" => "Paraguay","pe" => "Peru","ph" => "Philippines","pl" => "Poland","pt" => "Portugal","pr" => "Puerto Rico","qa" => "Qatar","ro" => "Romania","ru" => "Russian Federation","lc" => "Saint Lucia","pm" => "Saint Pierre And Miquelon","ws" => "Samoa","sa" => "Saudi Arabia","sl" => "Sierra Leone","sg" => "Singapore","sk" => "Slovakia","si" => "Slovenia","sb" => "Solomon Islands","so" => "Somalia","za" => "South Africa","es" => "Spain","lk" => "Sri Lanka","sd" => "Sudan","se" => "Sweden","ch" => "Switzerland","sy" => "Syrian Arab Republic","tw" => "Taiwan, Province Of China","tz" => "Tanzania, United Republic Of","th" => "Thailand","tg" => "Togo","to" => "Tonga","tt" => "Trinidad And Tobago","tn" => "Tunisia","tr" => "Turkey","tc" => "Turks And Caicos Islands","tv" => "Tuvalu","ug" => "Uganda","ua" => "Ukraine","uk" => "United Kingdom","us" => "United States","uy" => "Uruguay","ve" => "Venezuela","vn" => "Viet Nam","vg" => "Virgin Islands, British","vi" => "Virgin Islands, U.S.","ye" => "Yemen","yu" => "Yugoslavia","zw" => "Zimbabwe");

	if(!$type)
	{
		foreach ($c as $k => $v)
		{
			if($k == "$name") $selected = "selected";
			else $selected = "";

			if($k == "none") $v = NULL;

			$list .= "<option value='$k' $selected>$v</option>\n";
		}
	}
	else
		foreach ($c as $k => $v)
			if($k == "$name") $list = $v;

return $list;
}

////////////////////////////////////////////////////////
// Function:         find_authorization
// Description: Shows the authorization

function find_authorization($authorization)
{
	global $config;
	require "language/{$config['language']}.php";

	switch($authorization)
	{
		case 1: $user_authorization = $language['Authorization 1']; break;
		case 2: $user_authorization = $language['Authorization 2']; break;
		case 3: $user_authorization = $language['Authorization 3']; break;
		case 4: $user_authorization = $language['Authorization 4']; break;
	}

return $user_authorization;
}

////////////////////////////////////////////////////////
// Function:         find_age
// Description: Find age 

function find_age($age,$surplus=false)
{
	if(!$age == NULL)
	{
		if(!$surplus) $diff = (date("Y") - date("Y",$age));
		else $diff = ((date("Y") + $surplus) - date("Y",$age));

		if (mktime(0,0,0,date("m",$age),date("j",$age),date("Y")) <= time()) $finish = $diff;
		else $finish = $diff - 1;
	}
	else $finish = NULL;

return $finish;
}

////////////////////////////////////////////////////////
// Function:         find_avatar
// Description: Shows the avatar

function find_avatar($avatar_def)
{
	if(isset($avatar_def)) $avatar = "<img src='images/avatars/$avatar_def' border='0' alt=''>";
	else $avatar = NULL;

return $avatar;
}

////////////////////////////////////////////////////////
// Function:         find_flag
// Description: Shows the flag

function find_flag($flag_def,$location_def,$name=false,$none=false)
{
	if(!$flag_def) $flag = "<img src='images/flags/none.gif' alt='$location_def'>";
	elseif($flag_def == "none" && $none == true) $flag = NULL;
	else $flag = "<img src='images/flags/$flag_def.gif' alt='".find_country($flag_def,1)."'>";

	if($name == true)
	{
		if(!$flag_def) $flag .= " $location_def";
		elseif($flag_def == "none" && $none == true) $flag .= NULL;
		else $flag .= " ".find_country($flag_def,1)."";
	}

return $flag;
}

////////////////////////////////////////////////////////
// Function:         find_status
// Description: Shows the status

function find_status($status_def)
{
	global $config;
	require "language/{$config['language']}.php";

	if($status_def  == "banned") $status = "<font color='red'>{$language['Users view desc3']}</font>";
	elseif($status_def  == "activate") $status = "<font color='orange'>{$language['Users view desc2']}</font>";
	else $status = "<font color='green'>{$language['Users view desc']}</font>";

return $status;
}

////////////////////////////////////////////////////////
// Function:         arrow_legend
// Description: Shows the arrows by date

function arrow_legend($date_def)
{
	$stat_one = time() - 86400; // a day
	$stat_two = time() - 259200; // 3 days
	$stat_three = time() - 604800; // a week

	if ($date_def > $stat_one) $stat = 1;
	elseif ($date_def > $stat_two) $stat = 2;
	elseif ($date_def > $stat_three) $stat = 3;
	else $stat = 4;

return $stat;
}

////////////////////////////////////////////////////////
// Function:         paginate
// Description: Display the [ 1 2 3 ] and [1] [2] [3]

function paginate($num_pages, $cur_page, $link_to, $forum_type=false)
{
	$pages = array();
	$link_to_all = false;

	if ($num_pages <= 1)
	{
		if($forum_type == "true") $pages = array('<td class="pagelink">1</td>');
		else $pages = array('<strong><u>1</u></strong>');
	}
	else
	{
		if ($cur_page > 3)
		{
			if($forum_type == "true")
			{
				$pages[] = '<td class="pagelink"><a href="'.$link_to.'&amp;p=1">1</a></td>';
				if ($cur_page != 4) $pages[] = '<td class="pagelink">&hellip;</td>';
			}
			else
			{
				$pages[] = '<a href="'.$link_to.'&amp;p=1">1</a>';
				if ($cur_page != 4) $pages[] = '&hellip;';
			}
		}

		for ($current = $cur_page - 2, $stop = $cur_page + 3; $current < $stop; ++$current)
		{
			if ($current < 1 || $current > $num_pages) continue;
			elseif ($current != $cur_page || $link_to_all)
			{
				if($forum_type == "true") $pages[] = '<td class="pagelink"><a href="'.$link_to.'&amp;p='.$current.'">'.$current.'</a></td>';
				else $pages[] = '<a href="'.$link_to.'&amp;p='.$current.'">'.$current.'</a>';
			}
			else
			{
				if($forum_type == "true") $pages[] = '<td class="pagecurrent">'.$current.'</td>';
				else $pages[] = '<strong><u>'.$current.'</u></strong>';
			}
		}

		if ($cur_page <= ($num_pages-3))
		{
			if($cur_page != ($num_pages-3))
			{
				if($forum_type == "true") $pages[] = '<td class="pagelink">&hellip;</td>';
				else $pages[] = '&hellip;';
			}

			if($forum_type == "true") $pages[] = '<td class="pagelink"><a href="'.$link_to.'&amp;p='.$num_pages.'">'.$num_pages.'</a></td>';
			else $pages[] = '<a href="'.$link_to.'&amp;p='.$num_pages.'">'.$num_pages.'</a>';
		}
	}

if($forum_type == "true") return implode('<td>&nbsp;</td>', $pages);
else return implode('&nbsp;', $pages);
}

////////////////////////////////////////////////////////
// Function:         form_dropdown
// Description: dropdown list from directory

function form_dropdown($name, $list=array(), $default_val, $slot1, $slot2, $slot3)
{
	if(!isset($output)) $output = NULL;
	$output .= "<select name='$name' $slot1 $slot2 $slot3>";

	foreach ($list as $k => $v)
	{
		$selected = "";

		if ($default_val != "" && $v[0] == $default_val) $selected = ' selected';

		$output .= "<option value='".$v[0]."'".$selected.">".$v[1]."</option>";
	}

	$output .= "</select>";

return $output;
}

?>
